using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using LeisureCenter.Core;
using LeisureCenter.Forms;

namespace LeisureCenter.OptionsForms
{
    public partial class FileSystemFolderOptionsDialog : DialogControl
    {
        private string path;

        public FileSystemFolderOptionsDialog(string path)
        {
            InitializeComponent();
            this.path = path;
            LibraryId = DatabaseManager.ExecuteLongScalar(string.Format(@"SELECT libraryId FROM Paths WHERE name = '{0}'", path), 0);
        }

        private long libraryId;

        public long LibraryId
        {
            get { return libraryId; }
            set
            {
                libraryId = value;
                if (value == 0)
                    btnLibrary.Text = @"<<None>>";
                else
                    btnLibrary.Text = DatabaseManager.ExecuteStringScalar(@"SELECT name FROM Libraries WHERE id = " + libraryId, @"<<none>>");
            }
        }

        private void btnLibrary_Click(object sender, EventArgs e)
        {
            List<string> items = new List<string>();
            List<long> ids = new List<long>();

            items.Add(@"<<None>>");

            IEnumerable<KeyValuePair<string, long>> libraries = DatabaseManager.ExecuteStringsLongsQuery("SELECT name, id FROM Libraries");
            foreach (KeyValuePair<string, long> library in libraries)
            {
                items.Add(library.Key);
                ids.Add(library.Value);
            }
            
            items.Add(@"Create new...");

            long newLibraryId = 0;

            int index = items.IndexOf(btnLibrary.Text);
            if (ListDialog.Execute(items, true, ref index, Color.White))
            {
                if (index == items.Count - 1)
                {
                    string name = "";
                    if (VirtualKeyboardDialog.Execute(@"Enter the name of the new library:", ref name) && !string.IsNullOrEmpty(name))
                    {
                        newLibraryId = DatabaseManager.ExecuteLongScalar(string.Format(@"INSERT INTO Libraries (name) values ('{0}'); SELECT last_insert_rowid();", name), libraryId);
                    }
                }
                else
                    if (index > 0)
                    {
                        newLibraryId = ids[index - 1];
                    }
                if (newLibraryId != LibraryId)
                {
                    if (newLibraryId == 0)
                    {
                        Program.LeisureCenterInstance.libraryManager.RemovePath(this.path);
                    }
                    else
                    {
                        Program.LeisureCenterInstance.libraryManager.AddPath(this.path, newLibraryId);
                    }

                    LibraryId = newLibraryId;
                }
            }
        }

        public override void SetFocusHandlers(LeisureCenter.Api.IFocusHandler okButton, LeisureCenter.Api.IFocusHandler cancelButton)
        {
            btnLibrary.FocusDown = okButton;
        }
    }
}
